package com.hyjx.framework.common.util;

import com.hyjx.framework.service.ConfigManager;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.List;
import java.util.Properties;

public class EmailUtil {
    /**
     * 发送简单文字邮件
     *
     * @param receiveEmailAddress
     * @param subject
     * @param content
     * @param fromEmailAddress
     * @return
     */
    public static boolean sendSimpleEmail(String subject, String content, List<String> receiveEmailAddress) {
        String smtpServer = ConfigManager.getConfig("email.smtpserver");
        String emailUserName = ConfigManager.getConfig("email.username");
        String emailPassword = ConfigManager.getConfig("email.password");
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        //设置需要验证(给提供用户名、密码)  
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.host", smtpServer);
        Session session = Session.getInstance(props, null);
        session.setDebug(false);//打开debug模式，会打印发送细节到console
        Message message = new MimeMessage(session); // 实例化一个MimeMessage集成自abstract// Message 。参数为session
        boolean sendResult = false;
        StringBuffer receiveEmail = new StringBuffer();
        try {
            message.setFrom(new InternetAddress(emailUserName));
            message.setContent(content, "text/html;charset=utf8"); // 设置文本内容
            // 单一文本使用setText,Multipart复杂对象使用setContent
            message.setSubject(subject); // 设置标题
            InternetAddress[] emailAddress = new InternetAddress[receiveEmailAddress.size()];
            int i = 0;
            for (String address : receiveEmailAddress) {
                emailAddress[i] = new InternetAddress(address);
                receiveEmail.append(address + ";");
                i++;
            }
            message.setRecipients(Message.RecipientType.TO, emailAddress); // 设置接收方
            /**
             * 使用静态方法每次发送需要建立一个到smtp服务器的链接，你可以手动控制连接状态
             * ，通过session获得tansport，连接到mailserver
             * ，而session就可以使用Session.getDefaultInstance(props,null);获得
             */
            Transport transport = session.getTransport("smtp");
            transport.connect(smtpServer, emailUserName, emailPassword);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            sendResult = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
//		Map logMap = new HashMap();
//		logMap.put("log_id", CommonUtil.getUUID32());
//		logMap.put("receive_email", receiveEmail.toString());
//		logMap.put("subject", subject);
//		logMap.put("content", content);
//		logMap.put("send_time", new Date());
//		logMap.put("result", sendResult==true?"1":"0");
//		IBaseDao dao = (IBaseDao)SpringBeanUtil.getBean("baseDao");
//		dao.addObject("framework.insertEmailLog", logMap);
        return sendResult;
    }

    public static void main(String[] args) {
    }
}
